<template>
	<div class="messSty">
		<div>
			<el-input placeholder="请输入学生账号" v-model="mes.id" @keyup.enter.native="userlogin">
				<template slot="prepend">账号:</template>
			</el-input>
		</div>
		<br /><br />
		<div>
			<el-input placeholder="请输入密码" v-model="mes.passWord" @keyup.enter.native="userlogin">
				<template slot="prepend">密码:</template>
			</el-input>
		</div>
		<br /><br />

		<el-container>
			<el-col :span="10">
				<router-link>忘记密码</router-link>
				<router-link to="/login/findUserPassword" style="color: red;text-decoration: none;">忘记密码</router-link>
			</el-col><br />
			<el-col :span="2"></el-col>
			<el-col :span="2"></el-col>
			<el-col :span="10">
				<router-link to="/login/sign" style="text-decoration: none;">立即注册</router-link>
			</el-col>
		</el-container><br /><br />

		<el-row>
			<el-button type="primary" @click="userlogin"  style="width: 100%">登录</el-button>
		</el-row>
	</div>
</template>

<script>
	import Cookie from 'cookie_js'
	export default {
		name: 'userLogin',
		data() {
			return {
				mes: {
					id: '',
					passWord: '',
				}
			}
		},
		methods: {
			userlogin() {
				this.$axios({
						method: "post",
						url: "/LoginStudentUser",
						data: JSON.stringify(this.mes),
						headers: {
							"Content-Type": "application/json"
						},
					})
					.then((res) => {
						if (res.data.code == 412) {
							alert("用户名或密码错误")
						};
						if (res.data.code == 200) {
							//userName和密码正确就往cookie里面存入token
							Cookie.set("token", res.data.data.token, {
								user: "localhost"
							});

							// 将管理员id存入本地
							localStorage.setItem("userId", JSON.stringify(res.data.data.id));

							//路由跳转
							this.$router.push({
								name: "userIndex",
							});
						}
					})
					.catch((e) => {
						alert("登录失败")
						console.log(e);
					});
			}
		},

	}
</script>

<style scoped>
</style>
